package com.gaga.console.base.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分页参数
 *
 * @date 2020年02月14日16:19:36
 */
@Data
@ApiModel(value = "PageParams", description = "分页参数")
public class PageParams<T extends BaseEntity> {

    @NotNull(message = "查询对象model不能为空")
    @ApiModelProperty(value = "查询参数")
    private T model;

    @ApiModelProperty(value = "页面大小", example = "10")
    private long pageSize = 10;

    @ApiModelProperty(value = "当前页", example = "1")
    private long page = 1;

    @ApiModelProperty("扩展参数")
    private Map<String, Object> queryParams = new HashMap<>(1);

    /**
     * 支持多个字段排序，用法： eg.1, 参数：{order:"name,id", order:"descending,ascending" }。 排序： name desc, id asc eg.2, 参数：{order:"name", order:"descending,ascending" }。 排序： name desc eg.3, 参数：{order:"name,id", order:"descending" }。 排序： name desc
     *
     * @return
     */

    @JsonIgnore
    public IPage<T> buildPage() {
        PageParams<T> params = this;
        return new Page<T>(params.getPage(), params.getPageSize());
    }


    /**
     * 构建特殊查询条件，比如like查询 eq等
     */
    public void buildSpecWrap(AbstractWrapper wraps) {

        PageParams<T> params = this;

        if (params.getQueryParams() == null) {
            return;
        }
        Map<String, Object> mapQuery = params.getQueryParams();

        for (Map.Entry<String, Object> stringObjectEntry : mapQuery.entrySet()) {

            String key = stringObjectEntry.getKey();
            //证明这个地方需要作为查询条件
            if (key.startsWith("F_") || key.startsWith("ORDER_")) {

                String[] splitValue = key.split("_");
                if (splitValue.length != 3) {
                    throw new RuntimeException("拼接参数有误");
                }
                String columName = splitValue[1];
                String sqlKeyWord = splitValue[2];
                SFunction<Object, Object> columNameFunction = (SFunction<Object, Object>) o -> StrUtil.toUnderlineCase(columName);

                switch (sqlKeyWord) {
                    case QueryKeyWord.EQUAL:
                        wraps.eq(columNameFunction, stringObjectEntry.getValue());
                        break;
                    case QueryKeyWord.LIKE:
                        wraps.like(columNameFunction, stringObjectEntry.getValue());
                        break;
                    case QueryKeyWord.LEFT_LIKE:
                        wraps.likeLeft(columNameFunction, stringObjectEntry.getValue());
                        break;
                    case QueryKeyWord.RIGHT_LIKE:
                        wraps.likeRight(columNameFunction, stringObjectEntry.getValue());
                        break;
                    case QueryKeyWord.DESC:
                        wraps.orderByDesc(columNameFunction);
                        break;
                    case QueryKeyWord.ASC:
                        wraps.orderByAsc(columNameFunction);
                        break;
                    case QueryKeyWord.IN:
                        wraps.in(columNameFunction, Arrays.stream(String.valueOf(stringObjectEntry.getValue()).split(",")).collect(Collectors.toList()));
                        break;
                    default:
                        break;
                }
            }
        }
    }


    /**
     * 构建表格需要的结果集
     *
     * @return
     */
    @JsonIgnore
    public Map<String, Object> buildTableResult(IPage iPage) {
        Map<String, Object> reusltMap = new HashMap<>();
        reusltMap.put("total", iPage.getTotal());
        reusltMap.put("items", iPage.getRecords());
        reusltMap.put("pageNum", iPage.getCurrent());
        return reusltMap;
    }

}
